package org.jeecg.modules.simulator.factory.station;

import org.jeecg.modules.simulator.factory.station.json.Stationbs;
import org.jeecg.modules.simulator.model.line.station.equipment.StationConcourser;
import org.jeecg.modules.simulator.model.line.station.equipment.StationEntryExitGate;
import org.jeecg.modules.simulator.model.line.station.equipment.StationPlatform;
import org.jeecg.modules.simulator.model.line.station.station.SimpleStation;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.line.station.station.StationState;
import org.jeecg.modules.simulator.model.line.station.station.TransferStation;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: gp2
 * @description:
 * @author: renzhg
 * @create: 2024-08-29 14:07
 **/
public class StationFactory {
    /**
     * @description: 按线路分类
     *
     * @author:  renzhg
     * @date:  2024/8/30 11:03
     **/
    @Getter
    private Map<String,List<Station>> stationSimpleMap=new HashMap<>();

    /**
     * @description: 换乘站按站分类
     *
     * @author:  renzhg
     * @date:  2024/8/30 11:04
     **/
    @Getter
    private Map<String, TransferStation> stationTransferMap=new HashMap<>();
    @Getter
    private List<Stationbs> stationbsList =new ArrayList<>();

    StationLoader stationLoader=new StationJsonRead();


    public void loadData(){

        this.stationbsList = stationLoader.loadStation();
        aware2Station(stationbsList);
    }


    public void aware2Station(List<Stationbs> stationbsList){
        List<Station> stations=new ArrayList<>();
        List<Station> transferStations=new ArrayList<>();
        for (Stationbs stationbs : stationbsList) {

            if(!stationbs.isTransfer()){
                Station station = new SimpleStation(stationbs.getLineId(), stationbs.getName());
                station.setId(stationbs.getId());
                station.setStartPt(stationbs.getStartOffset());
                station.setEndPt(stationbs.getEndOffset());
                setSEE(stationbs, station);
                setPlatform(stationbs, station);
                setStationConcourser(stationbs, station);
                station.setOpen(stationbs.isOpen());
                station.setTransferStation(false);
                stations.add(station);

            }else{
                Station station = new SimpleStation(stationbs.getLineId(), stationbs.getName());
                station.setId(stationbs.getId());
                station.setStartPt(stationbs.getStartOffset());
                station.setEndPt(stationbs.getEndOffset());
                setSEE(stationbs, station);
                setPlatform(stationbs, station);
                setStationConcourser(stationbs, station);
                station.setOpen(stationbs.isOpen());
                station.setTransferStation(true);
                transferStations.add(station);
            }


        }


        stationSimpleMap = stations.stream()
                .collect(Collectors.groupingBy(Station::getLine));

        Map<String, List<Station>> transmap = transferStations.stream()
                .collect(Collectors.groupingBy(Station::getName));

        for (Map.Entry<String, List<Station>> entry : transmap.entrySet()) {
            TransferStation transferStation = new TransferStation(entry.getKey());

            entry.getValue().forEach(station -> {

                transferStation.getTransferStationMap().put(station.getLine(), station);
                station.setTransferStation(true);
            });
            stationTransferMap.put(transferStation.getName(), transferStation);
//            stations.add(transferStation);
        }





    }



    public void setStationConcourser(Stationbs stationbs, Station station){
        Hall hall = stationbs.getHall();
        StationConcourser stationConcourse = new StationConcourser();
        stationConcourse.setArea(hall.getArea());
        stationConcourse.setCrossTime(hall.getCrossTime());
        stationConcourse.setLimitSpeed(hall.getLimitSpeed());
        stationConcourse.setOpen(hall.isOpen());
        station.setStationConcourser(stationConcourse);
    }


    /**
     * @description: 站台设置
     *
     * @author:  renzhg
     * @date:  2024/8/30 9:59
     **/
    private void setPlatform(Stationbs stationbs, Station station) {
        Platform platform = stationbs.getPlatform();
        StationPlatform stationPlatform = new StationPlatform();
        stationPlatform.setPlatformName(stationbs.getName());
        stationPlatform.setArea(platform.getArea());
        stationPlatform.setCrossTime(platform.getCrossTime());
        stationPlatform.setDownLimitSpeed(platform.getDownLimitSpeed());
        stationPlatform.setUpLimitSpeed(platform.getUpLimitSpeed());
        if(platform.getDownstate().equals("running")){
            stationPlatform.setDownstate(StationState.running);
        }else if(platform.getDownstate().equals("stop")) {
            stationPlatform.setDownstate(StationState.stop);
        }else if(platform.getDownstate().equals("limiting")) {
            stationPlatform.setDownstate(StationState.limiting);
        }

        if(platform.getUpstate().equals("running")){
            stationPlatform.setUpstate(StationState.running);
        }else if(platform.getUpstate().equals("stop")) {
            stationPlatform.setUpstate(StationState.stop);
        }else if(platform.getUpstate().equals("limiting")) {
            stationPlatform.setUpstate(StationState.limiting);
        }
        stationPlatform.setOpen(platform.isOpen());
        station.setStationPlatform(stationPlatform);

    }


    /**
     * @description: 设置进出口
     *
     * @author:  renzhg
     * @date:  2024/8/30 9:29
     **/
    private void setSEE(Stationbs stationbs, Station station) {
        stationbs.getInorouts().forEach(inorout -> {
            StationEntryExitGate stationEntryExitGate = new StationEntryExitGate();
            stationEntryExitGate.setEegName(inorout.getName());

            stationEntryExitGate.setBelongStationId(inorout.getStationId());
            stationEntryExitGate.setEntrySpeed(inorout.getDefaultInSpeed());
            stationEntryExitGate.setExitSpeed(inorout.getDefaultOutSpeed());

            extracted(inorout, stationEntryExitGate);
            stationEntryExitGate.setLimitSpeed(inorout.getLimitSpeed());
            station.getEntryExitGateMap().put(stationEntryExitGate.getEegName(),stationEntryExitGate);
        });
    }

    private  void extracted(InOrOut inorout, StationEntryExitGate stationEntryExitGate) {
        if(inorout.getOpen().equals("running")){
            stationEntryExitGate.setStationState(StationState.running);
        }else if(inorout.getOpen().equals("stop")) {
            stationEntryExitGate.setStationState(StationState.stop);
        }else if(inorout.getOpen().equals("limiting")) {
            stationEntryExitGate.setStationState(StationState.limiting);
        }
    }


}
